//
//  MyViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/16.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "MyViewController.h"
#import "MySettingViewController.h"
#import "MyTableViewCell.h"
#import "MyAvatarTableViewCell.h"
#import "FeedbackViewController.h"
#import "MyInfoViewController.h"
#import "MyResumeViewController.h"
#import "MyMessageViewController.h"
#import "MyDraftViewController.h"

static NSString * const MY_CELL = @"myCell";
static NSString * const MY_AVATAR_CELL = @"myAvatarCell";

@interface MyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTV;
@property (nonatomic, strong) NSArray *iconArr;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSDictionary *userInfo;

@end

@implementation MyViewController

- (void) viewWillAppear:(BOOL)animated {
    self.userInfo = [[TMCache sharedCache] objectForKey:@"userInfo"];
    [_myTV reloadData];
}

- (void) viewDidAppear:(BOOL)animated {
    if (_isPopRoot) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }else{
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self customizeBackItem];
    __weak typeof (self) weakSelf = self;
    [self customizeBackItemWithCallBack:^{
        if (_isPopRoot) {
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    [self getProfile];
    [self setStyle];
}

- (void) setStyle {
    self.navigationItem.title = @"我的";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(pushSettingVC)];
    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    
    self.view.backgroundColor = GRAY_COLOR;
    
    self.myTV = [UITableView newAutoLayoutView];
    [self.view addSubview:_myTV];
    
    [_myTV autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [_myTV autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_myTV autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    [_myTV autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    
    _myTV.delegate = self;
    _myTV.dataSource = self;
    _myTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTV.backgroundColor = GRAY_COLOR;
    
    [_myTV registerClass:[MyTableViewCell class] forCellReuseIdentifier:MY_CELL];
    [_myTV registerClass:[MyAvatarTableViewCell class] forCellReuseIdentifier:MY_AVATAR_CELL];
    
//    self.iconArr = @[@[@"", @"myResumeICON", @"draftICON"],
//                     @[@"messageICON"],
//                     @[@"feedBackICON", @"assessICON"]];
    
    self.iconArr = @[@[@"", @"myResumeICON", @"draftICON"],
                     @[@"messageICON"],
                     @[@"feedBackICON"]];
    
//    self.titleArr = @[@[@"", @"我的简历", @"草稿箱"],
//                      @[@"消息"],
//                      @[@"意见反馈", @"给个好评吧"]];
    
    self.titleArr = @[@[@"", @"我的简历", @"草稿箱"],
                      @[@"消息"],
                      @[@"意见反馈"]];
    
    self.userInfo = [[TMCache sharedCache] objectForKey:@"userInfo"];
    
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }else if(section == 1){
        return 1;
    }else{
        return [self.titleArr[2] count];
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    tableView.backgroundColor = GRAY_COLOR;
    if (section == 0) {
        return 0;
    }else{
        return 10;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 140;
    }else{
        return 50;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        MyAvatarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MY_AVATAR_CELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nickNameLB.text = [_userInfo objectForKey:@"nickname"];
        cell.signLB.text = [_userInfo objectForKey:@"sign"];
        cell.backgroundColor = GRAY_COLOR;
        NSURL *imageUrl = [NSURL URLWithString:[_userInfo objectForKey:@"avatar"]];
        [cell.avatarImg sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"logoOrange"]];
        
        return cell;
    }else{
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MY_CELL forIndexPath:indexPath];
        
        cell.iconImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", _iconArr[indexPath.section][indexPath.row]]];
        cell.titleLB.text = [NSString stringWithFormat:@"%@", _titleArr[indexPath.section][indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        NSDictionary *tmpDic = [[TMCache sharedCache] objectForKey:@"userInfo"];
        
        if (indexPath.section == 0) {
            
            cell.badgeLB.hidden = NO;
            cell.msgBadgeLB.hidden = YES;
            if (indexPath.row == 1) {
                cell.badgeLB.text = [[tmpDic objectForKey:@"resume_total"] objectForKey:@"normal"];
            }else{
                cell.badgeLB.text = [[tmpDic objectForKey:@"resume_total"] objectForKey:@"draft"];
            }
        }else if (indexPath.section == 1){
            NSString *msgNum = [tmpDic objectForKey:@"new_message"];
            if ([msgNum isEqualToString:@"0"]) {
                cell.msgBadgeLB.hidden = YES;
            }else{
                cell.msgBadgeLB.hidden = NO;
                cell.msgBadgeLB.text = msgNum;
            }
            
            cell.badgeLB.hidden = YES;
        }else{
            cell.msgBadgeLB.hidden = YES;
            cell.badgeLB.hidden = YES;
        }
        
        return cell;
    }
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [MobClick event:@"Click_My_AvatarBTN"];

            MyInfoViewController *myInfoVC = [[MyInfoViewController alloc] init];
            [self.navigationController pushViewController:myInfoVC animated:YES];
        }else if(indexPath.row == 1){
            [MobClick event:@"Click_My_myResumeBTN"];

            MyResumeViewController *myResumeVC = [[MyResumeViewController alloc] init];
            [self.navigationController pushViewController:myResumeVC animated:YES];
        }else{
            [MobClick event:@"Click_My_myDraftResumeBTN"];

            MyDraftViewController *myDraftVC = [[MyDraftViewController alloc] init];
            [self.navigationController pushViewController:myDraftVC animated:YES];
        }
    }else if (indexPath.section == 1){
        [MobClick event:@"Click_My_MessageBTN"];

        MyMessageViewController *myMessageVC = [[MyMessageViewController alloc] init];
        [self.navigationController pushViewController:myMessageVC animated:YES];
    }else{
        if (indexPath.row == 0) {
            [MobClick event:@"Click_My_FeedbackBTN"];

            FeedbackViewController *feedBackVC = [[FeedbackViewController alloc] init];
            [self.navigationController pushViewController:feedBackVC animated:YES];
        }else{
            // 给好评
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_DOWNLOAD_URL]];
        }
    }
}

#pragma mark - 获取用户信息
- (void) getProfile {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[[TMCache sharedCache] objectForKey:@"SSID"] forKey:@"ssid"];
    
    [SVProgressHUD show];
    [[HttpManager instance] requestWithMethod:@"User/getProfile"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {

                                          [[TMCache sharedCache] setObject:[result objectForKey:@"data"] forKey:@"userInfo"];
                                          
                                          self.titleArr = [[[result objectForKey:@"data"] objectForKey:@"cell_show"] objectForKey:@"name"];
                                        self.iconArr = [[[result objectForKey:@"data"] objectForKey:@"cell_show"] objectForKey:@"icon"];
                                          
                                          [_myTV reloadData];
                                          [SVProgressHUD dismiss];
                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

#pragma mark - 设置页面
- (void) pushSettingVC {
    [MobClick event:@"Click_My_SettingBTN"];

    MySettingViewController *mySettingVC = [[MySettingViewController alloc] init];
    [self.navigationController pushViewController:mySettingVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
